package com.sinven.util;

import com.sinven.common.ErrorMessage;
import com.sinven.common.UploadResult;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Calendar;
import java.util.UUID;

@Slf4j
public class UploadUtil {
    /**
     * 封装上传成功结果
     * @param fileName 上传结果文件名
     * @param url 图片的访问地址
     * @return UploadResult
     */
    public static UploadResult success(String fileName,String url){
        UploadResult result = new UploadResult();
        result.setFileName(fileName);
        result.setUploaded(1);
        result.setUrl(url);
        return result;
    }

    /**
     * 封装上传错误结果
     * @param message 错误信息
     * @return UploadResult
     */
    public static UploadResult error(String message){
        UploadResult result = new UploadResult();
        result.setUploaded(0);
        ErrorMessage error = new ErrorMessage();
        error.setMessage(message);
        result.setError(error);
        return result;
    }

    /**
     * 生成文件实际存储
     *<p>文件存储路径为基路径下以年份为第一级目录，以月份为第二集目录</p>
     * @param baseDirectory    文件存储基目录
     * @param originalFilename 原文件名
     * @return 磁盘文件名
     */
    public static File generateStorageFile(String baseDirectory, String originalFilename) {
        log.debug("上传文件存储基目录:{}", baseDirectory);
        log.debug("上传文件原文件名:{}", originalFilename);
        //判断服务器当前环境是windows还是linux
        baseDirectory = baseDirectory.startsWith("file:")?baseDirectory.replace("file:",""):baseDirectory;
        baseDirectory = baseDirectory.startsWith("classpath:")?baseDirectory.replace("classpath:",""):baseDirectory;
        Calendar calendar = Calendar.getInstance();
        String firstPath = calendar.get(Calendar.YEAR) + "";
        String secondPath = calendar.get(Calendar.MONTH) + 1 + "";
        String suffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "");

        StringBuilder sbDestFile = new StringBuilder(baseDirectory);
        sbDestFile.append(firstPath);
        sbDestFile.append(File.separator);
        sbDestFile.append(secondPath);
        sbDestFile.append(File.separator);
        sbDestFile.append(newFileName);
        sbDestFile.append(suffix);
        log.debug("上传文件存储路径为:{}", sbDestFile.toString());
        File dest = new File(sbDestFile.toString());
        if (!dest.getParentFile().exists()) {
            boolean b = dest.getParentFile().mkdirs();
            if(!b){
                log.debug("父级路径创建失败");
            }
        }
        return dest;
    }

    /**
     * 获取绝对文件的相对路径
     * @param baseDirectory 文件存储基础目录<b>只需要基础目录名，请勿给绝对路径</b>
     * @param absoluteFile 绝对文件目录
     * @return 文件的相对路径
     */
    public static String getRelativeFileName(String baseDirectory,File absoluteFile){
        log.debug(absoluteFile.getName());
        String absolutePath = absoluteFile.getAbsolutePath();
        log.debug("文件绝对路径为{}",absolutePath);
        String relativePath = absolutePath.substring(absolutePath.lastIndexOf(baseDirectory));
        if ("\\".equals(File.separator)){
            log.debug("服务器运行环境为Windows");
            relativePath=relativePath.replaceAll("\\\\","/");
        }
        log.debug("文件相对路径为{}",relativePath);
        return  relativePath;
    }
}
